package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class OreWar extends BaseRepair {

    public OreWar(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void go(Map<Integer,List<Long>> activityIdUserIdMap){
        Map<Integer,Map<Long,Map<String, String>>> listMap = new ConcurrentHashMap();
        for(Integer activityId : activityIdUserIdMap.keySet()){
            List<Long> userIdList = activityIdUserIdMap.get(activityId);
            Map<Long,Map<String, String>> resMap = getLog(activityId,userIdList);
            listMap.put(activityId,resMap);
        }
        DataMgr.addUserConditionData(listMap);
    }


    private Map<Long,Map<String, String>> getLog(Integer activityId,List<Long> userIdList) {
        Map<Long,Map<String, String>> res = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String Condition = "userId in(" + StringUtils.listToString(userIdList, ",") + ")";
        String sql = "SELECT * FROM t_u_activity_orewar_condition WHERE `type` = 2 AND activityId = ? AND " + Condition;
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                temp.put("value",""+ checkNullToEmpty(rs.getString("value")));
                res.put(rs.getLong("userId"),temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return res;
    }
}
